package demo.learnspringboot.Controller;


import demo.learnspringboot.Properities.jwtProperty;
import demo.learnspringboot.Result.result;
import demo.learnspringboot.Service.userService;
import demo.learnspringboot.VO.userLoginVO;

import demo.learnspringboot.dto.userLoginDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import demo.learnspringboot.Utilize.jwtUtil;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@Tag(name = "首页")
public class LoginController {

    @Autowired
    userService userService;

    @Autowired
    jwtProperty jwtProperty;

    /*
     **登陆
     ** @param  userDTO
     ** @return
     */
    @GetMapping("/admin/login")
    @Operation(summary = "用户登陆功能")
    public result<userLoginVO> Login(userLoginDTO userLoginDTO) {
        log.info("登陆：{}", userLoginDTO);
        //登陆操作
        String result = userService.Login(userLoginDTO);

//        加jwt
//        创建claims，以账号名字作为自定义内容
        Map<String, Object> claims = new HashMap<>();
        claims.put("username", userLoginDTO.getUsername());
        jwtUtil jwtUtil = new jwtUtil();
//        输入claims，密钥，时间来创建jwt
        String tokens = jwtUtil.createJWT(claims, jwtProperty.getSecretKey(), jwtProperty.getTime());
        userLoginVO userLoginVO = new userLoginVO(userLoginDTO.getUsername(), "****", tokens);

        return demo.learnspringboot.Result.result.success(userLoginVO);
    }


}
